import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/route/route.dart';
import 'package:meal/ui/draw_under_line.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';

///
/// 已报名活动
///
class MarketRegisteredActivity extends StatefulWidget {
  final bool isAll;

  const MarketRegisteredActivity({Key? key, this.isAll = true}) : super(key: key);

  @override
  State<MarketRegisteredActivity> createState() => _MarketRegisteredActivityState();
}

class _MarketRegisteredActivityState extends State<MarketRegisteredActivity> {
  late ScrollController scrollController;

  bool isLoading = false;

  bool noMore = false;

  TextStyle get defaultStyle {
    return StylesUtils.customTextStyle(
        color: ColorsUtils.colorFb8, fontSize: Sizes.default13, lineHeight: 1.5);
  }

  TextStyle get style {
    return StylesUtils.customTextStyle(color: ColorsUtils.colorFb8, fontSize: Sizes.default15);
  }

  @override
  void initState() {
    super.initState();
    scrollController = ScrollController()
      ..addListener(() {
        if (!isLoading &&
            !noMore &&
            scrollController.position.pixels >= scrollController.position.maxScrollExtent) {
          setState(() {
            isLoading = true;
            loadMoreData();
          });
        }
      });
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          Container(
            margin: const EdgeInsets.all(Sizes.default20),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(Sizes.default10)),
            child: ListBody(
              children: [
                DrawUnderLine(
                  color: ColorsUtils.parseColorFromHexString('#D7D7D7'),
                  child: Container(
                    padding: const EdgeInsets.only(
                        bottom: Sizes.default20,
                        top: Sizes.default10,
                        left: Sizes.default15,
                        right: Sizes.default15),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                          flex: 1,
                          child: Container(
                            alignment: Alignment.topLeft,
                            child: Text(
                              'No.12',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color797),
                            ),
                          ),
                        ),
                        Expanded(
                          flex: 5,
                          child: Container(
                            padding: const EdgeInsets.only(left: Sizes.default10),
                            child: Column(
                              children: [
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '满500P减50P',
                                    style: StylesUtils.customTextStyle(
                                        fontSize: Sizes.default16, color: ColorsUtils.colorFf6),
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                  child: Text(
                                    '活动细则：满500P减50P（减免部分，平台承担10P，商家承担40P）',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '用户类型：所有用户',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                  child: Text(
                                    '使用方式：移动端',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '活动日期：2021-11-01 至 2021-11-30',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.only(top: Sizes.default10),
                                  child: Text(
                                    '报名日期：2021-11-01 至 2021-11-30',
                                    style: defaultStyle,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
                Container(
                  height: Sizes.default60,
                  alignment: Alignment.centerRight,
                  child: TextButton(
                      onPressed: gotoView,
                      child: Text('使用详情', style: style)),
                )
              ],
            ),
          ),
          Container(
            margin: const EdgeInsets.only(
                left: Sizes.default20, right: Sizes.default20, bottom: Sizes.default20),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(Sizes.default10)),
            child: ListBody(
              children: [
                DrawUnderLine(
                  color: ColorsUtils.parseColorFromHexString('#D7D7D7'),
                  child: Container(
                    padding: const EdgeInsets.only(
                        bottom: Sizes.default20,
                        top: Sizes.default10,
                        left: Sizes.default15,
                        right: Sizes.default15),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                          flex: 1,
                          child: Container(
                            alignment: Alignment.topLeft,
                            child: Text(
                              'No.11',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color797),
                            ),
                          ),
                        ),
                        Expanded(
                          flex: 5,
                          child: Container(
                            padding: const EdgeInsets.only(left: Sizes.default10),
                            child: Column(
                              children: [
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '免基础配送费',
                                    style: StylesUtils.customTextStyle(
                                        fontSize: Sizes.default16, color: ColorsUtils.colorFf6),
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                  child: Text(
                                    '活动细则：满500P减50P（减免部分，平台承担10P，商家承担40P）',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '用户类型：所有用户',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                  child: Text(
                                    '使用方式：移动端',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '活动日期：2021-11-01 至 2021-11-30',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.only(top: Sizes.default10),
                                  child: Text(
                                    '报名日期：2021-11-01 至 2021-11-30',
                                    style: defaultStyle,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
                Container(
                  height: Sizes.default60,
                  alignment: Alignment.centerRight,
                  child: TextButton(onPressed: gotoView, child: Text('使用详情', style: style)),
                )
              ],
            ),
          ),
          Container(
            margin: const EdgeInsets.only(
                left: Sizes.default20, right: Sizes.default20, bottom: Sizes.default20),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(Sizes.default10)),
            child: ListBody(
              children: [
                DrawUnderLine(
                  color: ColorsUtils.parseColorFromHexString('#D7D7D7'),
                  child: Container(
                    padding: const EdgeInsets.only(
                        bottom: Sizes.default20,
                        top: Sizes.default10,
                        left: Sizes.default15,
                        right: Sizes.default15),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                          flex: 1,
                          child: Container(
                            alignment: Alignment.topLeft,
                            child: Text(
                              'No.10',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color797),
                            ),
                          ),
                        ),
                        Expanded(
                          flex: 5,
                          child: Container(
                            padding: const EdgeInsets.only(left: Sizes.default10),
                            child: Column(
                              children: [
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '五折钜惠商家',
                                    style: StylesUtils.customTextStyle(
                                        fontSize: Sizes.default16, color: ColorsUtils.colorFf6),
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                  child: Text(
                                    '活动细则：外卖订单五折优惠（不含团购订单、到店自取订单）',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '店铺要求：限早餐品类',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                  child: Text(
                                    '用户类型：新注册用户',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '使用方式：移动端',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                  child: Text(
                                    '活动日期：3个月',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '报名日期：长期',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.only(top: Sizes.default10),
                                  child: Text(
                                    '报名费用：￥5.00',
                                    style: defaultStyle,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
                Container(
                  height: Sizes.default60,
                  alignment: Alignment.centerRight,
                  child: TextButton(
                      onPressed: gotoView,
                      child: Text('使用详情', style: style)),
                )
              ],
            ),
          ),
          Container(
            margin: const EdgeInsets.only(
                left: Sizes.default20, right: Sizes.default20, bottom: Sizes.default20),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(Sizes.default10)),
            child: ListBody(
              children: [
                DrawUnderLine(
                  color: ColorsUtils.parseColorFromHexString('#D7D7D7'),
                  child: Container(
                    padding: const EdgeInsets.only(
                        bottom: Sizes.default20,
                        top: Sizes.default10,
                        left: Sizes.default15,
                        right: Sizes.default15),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                          flex: 1,
                          child: Container(
                            alignment: Alignment.topLeft,
                            child: Text(
                              'No.09',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color797),
                            ),
                          ),
                        ),
                        Expanded(
                          flex: 5,
                          child: Container(
                            padding: const EdgeInsets.only(left: Sizes.default10),
                            child: Column(
                              children: [
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '新店八折特惠',
                                    style: StylesUtils.customTextStyle(
                                        fontSize: Sizes.default16, color: ColorsUtils.colorFf6),
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                  child: Text(
                                    '活动细则：外卖订单五折优惠（不含团购订单、到店自取订单）',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '店铺要求：限早餐品类',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                  child: Text(
                                    '用户类型：新注册用户',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '使用方式：移动端',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                  child: Text(
                                    '活动日期：3个月',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  child: Text(
                                    '报名日期：长期',
                                    style: defaultStyle,
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topLeft,
                                  margin: const EdgeInsets.only(top: Sizes.default10),
                                  child: Text(
                                    '报名费用：￥5.00',
                                    style: defaultStyle,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
                Container(
                  height: Sizes.default60,
                  alignment: Alignment.centerRight,
                  child: TextButton(
                      onPressed: gotoView,
                      child: Text('使用详情', style: style)),
                )
              ],
            ),
          ),
          renderBottom(),
        ],
      ),
    );
  }

  void gotoView() {
    Get.toNamed(RouteGet.marketUseActivity);
  }

  Widget renderBottom() {
    if (noMore) {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        alignment: Alignment.center,
        child: Text(
          '- 已显示全部数据 -',
          style:
              StylesUtils.customTextStyle(color: ColorsUtils.color797, fontSize: Sizes.default15),
        ),
      );
    }
    if (isLoading) {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '努力加载中...',
              style: StylesUtils.customTextStyle(
                  color: ColorsUtils.color797, fontSize: Sizes.default15),
            ),
            const Padding(padding: EdgeInsets.only(left: 10)),
            const SizedBox(
              width: 20,
              height: 20,
              child: CircularProgressIndicator(strokeWidth: 3),
            ),
          ],
        ),
      );
    } else {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        alignment: Alignment.center,
        child: Text(
          '上拉加载更多',
          style:
              StylesUtils.customTextStyle(color: ColorsUtils.color797, fontSize: Sizes.default15),
        ),
      );
    }
  }

  void loadMoreData() {
    Future.delayed(const Duration(seconds: 1), () {
      setState(() {
        noMore = true;
        isLoading = false;
      });
    });
  }
}
